package com.app.controller;

import cn.hutool.core.util.StrUtil;
import com.app.enums.StatusCodeEnum;
import com.app.mapper.CategoryMapper;
import com.app.model.CategoryModel;
import com.app.model.EntrepreneurshipModel;
import com.app.model.MapModel;
import com.app.model.UserModel;
import com.app.service.AdminMapService;
import com.app.service.EntrepreneurshipService;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 创业“蓝海”
 */
@Controller
@RestController
@RequestMapping("/api")
public class EntrepreneurshipController {
    @Autowired
    private EntrepreneurshipService entrepreneurshipService;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private AdminMapService adminMapService;


    /**
     * 查询商户地图
     *
     * @param request 请求
     * @param id      id
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/queryMerchantsDetails")
    public Result<Object> queryMerchantsMap(@RequestParam(value = "id") Integer id,
                                                HttpServletRequest request)
    {
        MapModel mapModel=this.adminMapService.selectById(id);
        if(mapModel.getType()!=null)
        {
            CategoryModel categoryModel=categoryMapper.selectById(mapModel.getType());
            if(categoryModel!=null)
            {
                mapModel.setTypeText(categoryModel.getName());
            }
        }
        return  Result.success(mapModel);
    }




    //类型列表
    @GetMapping("/entrepreneurshipTypeList")
    public Result<Object> entrepreneurshipTypeList() {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 1);
        return Result.success(categoryMapper.selectList(queryWrapper));
    }

    //新增和编辑
    @PostMapping("/insertAndUpdateEntrepreneurship")
    public Result<Object> insertAndUpdateVideo(@RequestBody EntrepreneurshipModel model, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS401.getCode(), StatusCodeEnum.STATUS401.getMsg());
        }
        String mobile = TokenUtils.nothingness(token);
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }

        if (model.getId() > 0) {
            model.setUpdateTime(CommonHelp.getNowTimestamp());
            if (model.getStatus() == null) {
                model.setStatus(0);
            }
            entrepreneurshipService.update(model);
        } else {
            model.setCreateTime(CommonHelp.getNowTimestamp());
            model.setUserId(user.getId());
            model.setPhone(user.getPhone());
            entrepreneurshipService.insert(model);
        }
        return Result.success("操作成功");
    }


    //列表
    @GetMapping("/getEntrepreneurshipList")
    public Result<Object> getVideoList(Integer page, Integer limit, String keyword, Integer type, HttpServletRequest request,Integer township) {
        Double longitude = null;
        Double latitude = null;
        if (request.getHeader("longitude") != null) {
            longitude = Double.parseDouble(request.getHeader("longitude"));
        }
        if (request.getHeader("latitude") != null) {
            latitude = Double.parseDouble(request.getHeader("latitude"));
        }
        return Result.success(entrepreneurshipService.selectList(page, limit, keyword, type, longitude, latitude,township));

    }

    //删除
    @GetMapping("/deleteEntrepreneurship")
    public Result<Object> deleteVideo(Integer id) {
        entrepreneurshipService.deleteById(id);
        return Result.success("操作成功");
    }

    //查看详情
    @GetMapping("/getEntrepreneurshipInfo")
    public Result<Object> getVideoInfo(Integer id) {
        return Result.success(entrepreneurshipService.selectById(id));
    }

}
